﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Resources;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using System.Windows.Threading;

namespace TTApp.Phone.Silverlight.Hexagons
{
    public partial class MenuPage : PhoneApplicationPage
    {
        private DispatcherTimer timeCounterDt;

        #region //--------- Private Fields --------------//
        private Image _musicOnImage;
        private Image _musicOffImage;
        #endregion

        #region //--------- Public Constructors ---------//
        public MenuPage()
        {
            InitializeComponent();


            _musicOnImage = GetImage("Images/menu_music_on.png");
            _musicOffImage = GetImage("Images/menu_music_off.png");
            musicToggleImage.Source = _musicOnImage.Source;

            timeCounterDt = new System.Windows.Threading.DispatcherTimer();
            timeCounterDt.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
            timeCounterDt.Tick += new EventHandler(timeCounterDt_Tick);
            timeCounterDt.Start();

            if ((Application.Current as App).IsFirstTimeInGame)
            {
                (Application.Current as App).IsFirstTimeInGame = false;

                if (Microsoft.Xna.Framework.Media.MediaPlayer.State == Microsoft.Xna.Framework.Media.MediaState.Playing)
                {
                    MessageBoxResult result;
                    result = MessageBox.Show("The media player is currently playing.  Do you wish to stop it and continue?", "Continue", MessageBoxButton.OKCancel);
                    if (result == MessageBoxResult.OK)
                    {
                        try
                        {
                            FrameworkDispatcher.Update();
                            Microsoft.Xna.Framework.Media.MediaPlayer.Stop();
                            //SetIsolatedStorageSettings("isMediaPaused", true);
                            (Application.Current as App).IsPlayBackGroundMusic = true;

                        }
                        catch (Exception ex)
                        {
                            string a = ex.ToString();
                            MessageBox.Show(a);
                        }
                    }
                    else
                    {
                        // MessageBox.Show("cancel");
                        App app = Application.Current as App;
                        app.IsMute = true;
                        musicToggleImage.Source = _musicOffImage.Source;
                        (Application.Current as App).IsPlayBackGroundMusic = false;
                        //SetIsolatedStorageSettings("isMediaPaused", false);
                    }
                }
            }


        }
        #endregion

        #region //--------- Event Handlers --------------//

        void timeCounterDt_Tick(object sender, EventArgs e)
        {
            FrameworkDispatcher.Update();
        }
        private void SinglePlayerImageTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //this.NavigationService.Navigate(new Uri("/Pages/GameplayPage.xaml?gameplay=single", UriKind.Relative));
            this.NavigationService.Navigate(new Uri("/Pages/SingleGameplayPage.xaml", UriKind.Relative));
        }

        private void MultiPlayerImageTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //this.NavigationService.Navigate(new Uri("/Pages/GameplayPage.xaml?gameplay=multi", UriKind.Relative));
            this.NavigationService.Navigate(new Uri("/Pages/MultiGameplayPage.xaml", UriKind.Relative));
        }

        private void SettingImageTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            App app = Application.Current as App;
            if (app.IsMute)
            {
                app.IsMute = false;
                musicToggleImage.Source = _musicOnImage.Source;
            }
            else
            {
                app.IsMute = true;
                musicToggleImage.Source = _musicOffImage.Source;
            }
        }

        private void GuideImageTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Menu/Guide.xaml", UriKind.Relative));
        }

        private void AboutImageTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Menu/About.xaml", UriKind.Relative));
        }
        #endregion

        #region //--------- Overridden Methods ----------//
        protected override void OnNavigatedTo(NavigationEventArgs args)
        {
            loadingImage.Visibility = Visibility.Collapsed;
            ContentPanel.Visibility = Visibility.Visible;

            base.OnNavigatedTo(args);
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs args)
        {
            loadingImage.Visibility = Visibility.Visible;
            ContentPanel.Visibility = Visibility.Collapsed;

            base.OnNavigatingFrom(args);
        }
        #endregion

        #region //--------- Private Methods -------------//
        private Image GetImage(string path)
        {
            Image image = new Image();

            Uri uri = new Uri(String.Format("TTApp.Phone.Silverlight.Hexagons;component/{0}", path), UriKind.Relative);
            StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(resourceInfo.Stream);

            image.Source = bitmapImage;

            return image;
        }
        #endregion
    }
}